﻿using System.IO;

namespace Gonte.Workflow.Tasks
{
    public class WriteText
        : ITask
    {
        #region ITask Members

        public TaskResult Execute(TaskContext context)
        {
            ExceptionThrower.ThrowWhenNull("Writer", _writer);
            ExceptionThrower.ThrowWhenNull("Text", _text);

            TaskResult result = _writer.Execute(context);
            TextWriter writer = (TextWriter)result.Value;

            result = _text.Execute(context);
            string text = result.Value.ToString();

            Write(writer, text);
            return null;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Task that retrieves the writer
        /// </summary>
        public ITask Writer
        {
            get { return _writer; }
            set { _writer = value; }
        }

        /// <summary>
        /// Task that retrieves the text
        /// </summary>
        public ITask Text
        {
            get { return _text; }
            set { _text = value; }
        }

        #endregion

        #region Overridables

        public virtual void Write(TextWriter writer, string text)
        {
            writer.Write(text);
        }

        #endregion

        #region Fields

        private ITask _writer;
        private ITask _text; 

        #endregion
    }
}
